package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttachmentDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDiscountDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstDiscount;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文折扣审批节点
 * 
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosDiscountHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);

        List<KitePosDiscountDTO> discountList = new ArrayList<KitePosDiscountDTO>();
        List<KiteOfferInst> offerInsts = querySceneInstService.listMainOfferInsts(context.getSceneInstId());
        offerInsts.forEach(offerInst -> {
            List<KiteOfferInstDiscount> kiteOfferInstDiscounts = querySceneInstService
                .listOfferInstDiscounts(context.getCustId(), offerInst.getOfferInstId());
            if (Objects.isNull(kiteOfferInstDiscounts)) {
                // ErrorConsts.BUSI_CHECK_ERROR.throwOut("折扣审批信息不能为空");
                kiteOfferInstDiscounts = new ArrayList<>();
            }
            kiteOfferInstDiscounts = kiteOfferInstDiscounts.stream()
                .filter(discount -> !KiteStringUtils.equals(discount.getActionType().name(), ActionType.X.name()))
                .collect(Collectors.toList());
            String serviceOfferId = Optional.ofNullable(querySceneInstService.getSceneInst(context.getSceneInstId()))
                .map(KiteSceneInst::getServiceOfferId).orElse("");

            kiteOfferInstDiscounts.forEach(offerInstDiscount -> {
                KitePosDiscountDTO discount = new KitePosDiscountDTO();
                // 临时处理
                discount.setInstanceId(offerInstDiscount.getInstId());
                discount.setDiscountCode(offerInstDiscount.getInstId());
                if ("1013".equals(serviceOfferId)) {
                    discount.setInstanceId(SeqUtils.getNext("OFFER_INST_DISCOUNT", "DISCOUNT_INST_ID"));
                    discount.setDiscountCode(SeqUtils.getNext("OFFER_INST_DISCOUNT", "DISCOUNT_INST_ID"));
                }
                discount.setName(offerInstDiscount.getDiscountName());
                // discount.setType(offerInstDiscount.getDiscountType());
                discount.setDestItemId(offerInstDiscount.getDiscountOrderId());
                discount.setChangeType(ChangeTypeUtils.getChangeType(offerInstDiscount.getActionType().name()));
                List<KitePosAttachmentDTO> attachments = new ArrayList<KitePosAttachmentDTO>();
                KitePosAttachmentDTO attachement = new KitePosAttachmentDTO();
                // 服务协议
                attachement.setCatagory("13");
                String attachementName = null;
                String attachementUrl = offerInstDiscount.getDiscountFileUrl();
                if (KiteStringUtils.contains(offerInstDiscount.getDiscountFileUrl(), "*_*")) {
                    attachementName = KiteStringUtils.substring(offerInstDiscount.getDiscountFileUrl(),
                        KiteStringUtils.lastIndexOf(offerInstDiscount.getDiscountFileUrl(), "*_*") + 3);
                    attachementUrl = KiteStringUtils.substring(attachementUrl, 0,
                        KiteStringUtils.lastIndexOf(offerInstDiscount.getDiscountFileUrl(), "*_*"));
                }
                else {
                    attachementName = KiteStringUtils.substring(offerInstDiscount.getDiscountFileUrl(),
                        KiteStringUtils.lastIndexOf(offerInstDiscount.getDiscountFileUrl(), "/") + 1);
                }
                attachement.setName(attachementName);
                attachement.setUrl(attachementUrl);
                attachments.add(attachement);
                discount.setAttachments(attachments);
                discountList.add(discount);
            });
        });

        context.getPosDto().setDiscount(discountList);
    }

}
